package main.remote;

import main.Command;


public class TaskService implements MessageService {
	   public static final int TASK_SERVICE_MESSAGE = 100;
	   public static final int TASK_SERVICE_PORT = 1998;

	   public Message process(Message m) {
		   long startTime = System.currentTimeMillis();
		   System.err.println("SERVER>>> ");
		   String cmdName = m.getParam("cmd_name");
		   int cmdArg = m.getIntegerParam("cmd_arg");
		   //Command.execute(cmdName, cmdArg);
		   Command cmd = new Command(cmdName, cmdArg+"");
		   cmd.execute();
		   long endTime = System.currentTimeMillis();
			System.out.println("Time elapsed: " + (endTime - startTime)/1000);
		   m.setLongParam("result", endTime - startTime);
		   return m;
	   }

	   public static void main(String args[]) {
	      TaskService taskService = new TaskService();
	      Server server;
	      try {
	         server = new Server(TASK_SERVICE_PORT);
	      } catch (Exception e) {
	         System.err.println("Could not start service " + e);
	         return;
	      }
	      Thread tsThread = new Thread(server);
	      server.subscribe(TASK_SERVICE_MESSAGE, taskService);
	      tsThread.start();
	   }
	}